
#include <stdio.h>
#include <string.h>
#include "symbolTable.h"
#include "portal.y.c"
symbolTable *head;

symbolTable* createItem() {
	symbolTable *new;
	
	new = (symbolTable*) malloc(sizeof(symbolTable));
	new->next = NULL;
	new->prev = NULL;
	return new;
}

int isEmpty(symbolTable *table){
	if(table == NULL) return 1;
	else return 0;
}

symbolTable* TS_search(symbolTable **table, char *name, int level) {
	symbolTable *p;
	p = *table;
	while (p != NULL) {
		if (strcmp(p->name, name) && p->level == level) {
			return p;
		}
		p = p->prev;
	}
	return 0;
}

int TS_isAdded(symbolTable **table, char name[64], int level) {
	symbolTable *p;
	p = *table;
	while (p != NULL) {
		if (strcmp(p->name, name) && p->level == level) {
			return 1;
		}
		p = p->prev;
	}
	return 0;
}

int TS_update(symbolTable **table, symbolTable *item ) {
	symbolTable *p, *pprev, *pnext;
	if (isEmpty(*table)) {
		return -1;
	}
	else {
		if (isAdded(table, item->name, item->level)) {
			p = search(table, item->name, item->level);
			pprev = p->prev;
			pnext = p->next;
			p = item;
			p->prev = pprev;
			p->next = ppnext;
			return 0;
		}else{
		return -1;
		 }
	}
	return -1;
}

int TS_add(symbolTable **table, symbolTable *new ) {
	if (isEmpty(*table)) {
		new = createItem();
		*table = new;
		new->prev = NULL;
		return 0;
	}
	else {
		if (!isAdded(table, new->name, new->level)) {
			new = createItem();
			(*table)->next = new;
			new->prev = *table;
			*table = new;
			return 0;
		}
	}
	return -1;
}

void TS_Insere_Tipo(char *name, int type){
	symbolTable *p;
	p = search(name);
	p->type = type;
	return p;
}

int main() {
	symbolTable *head;
	printf("Comecou");
	mainParser();
	printf("\nAcabou\n");
	return 0;
}
	
